package com.zhuiYun.project.api.syslog.controller;


import com.zhuiYun.project.api.syslog.model.SysLogExceptionModel;
import com.zhuiYun.project.api.syslog.model.SysLogModel;
import com.zhuiYun.project.api.syslog.service.SysLogService;
import com.zhuiYun.project.common.controller.BaseController;
import com.zhuiYun.project.common.errorCode.CommonException;
import com.zhuiYun.project.util.pageUtil.PageRequest;
import com.zhuiYun.project.util.pageUtil.PageResult;
import com.zhuiYun.project.common.response.CommonResult;
import com.zhuiYun.project.api.syslog.service.SysLogExceptionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 日志操作服务
 * @auther zcy
 * @data 2023/3/22 13:35
 */
@RestController
@RequestMapping("/syslog")
@Api(tags = "日志操作模块")
public class SysLogController extends BaseController {

    @Autowired
    SysLogService sysLogService;

    @Autowired
    SysLogExceptionService sysLogExceptionService;

    /**
     * 保存操作日志
     * @param sysLogModel
     * @return
     * @throws CommonException
     */
    @PostMapping("/saveSyslog")
    @ApiOperation(value = "用于其他服务的日志调度")
    public CommonResult saveSyslog(SysLogModel sysLogModel) throws CommonException {
        return sysLogService.saveSyslog(sysLogModel);
    }

    /**
     * 分页查询日志
     * @return
     */
    @GetMapping("/selectSysLogByPage")
    @ApiOperation(value = "分页查询日志")
    public CommonResult selectSysLogByPage(PageRequest pageRequest, SysLogModel sysLogModel) {
        return CommonResult.ok(sysLogService.selectSysLogByPage(pageRequest, sysLogModel));
    }

    /**
     * 删除日志
     * @param id
     * @return
     */
    @DeleteMapping("/deleteSysLogById")
    @ApiOperation(value = "删除日志")
    public CommonResult deleteSysLogById(@ApiParam("主键id") Integer id) throws CommonException {
        return sysLogService.deleteSysLogById(id);
    }

    /**
     * 根据id查询日志详情
     * @param id
     * @return
     */
    @GetMapping("/selectSysLogById")
    @ApiOperation(value = "id查询日志")
    public CommonResult selectSysLogById(Integer id) throws CommonException {
        return CommonResult.ok(sysLogService.selectSysLogById(id));
    }


    /**
     * 分页查询异常日志
     * @return
     */
    @GetMapping("/selectSysLogExByPage")
    @ApiOperation(value = "分页查询异常日志")
    public CommonResult selectSysLogExByPage(PageRequest pageRequest, SysLogExceptionModel sysLogExceptionModel) {
        PageResult pageResult = sysLogExceptionService.selectSysLogExByPage(pageRequest, sysLogExceptionModel);
        return CommonResult.ok(pageResult);
    }

    /**
     * 异常日志删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteSysLogEXById")
    @ApiOperation(value = "删除异常日志")
    public CommonResult deleteSysLogEXById(Integer id) throws CommonException {
        return sysLogExceptionService.deleteSysLogEXById(id);
    }

    /**
     * 日志详情
     * @param id
     * @return
     */
    @GetMapping("/selectSysLogExById")
    @ApiOperation(value = "id查询日志详情")
    public CommonResult selectSysLogExById(Integer id) throws CommonException {
        return CommonResult.ok(sysLogExceptionService.selectSysLogExById(id));
    }

    /**
     * 保存异常日志
     * @param sysLogExceptionModel
     * @return
     * @throws CommonException
     */
    @PostMapping("/saveSysLogEx")
    @ApiOperation(value = "用于其他服务的异常日志调度")
    public CommonResult saveSysLogEx(SysLogExceptionModel sysLogExceptionModel) throws CommonException {
        return sysLogExceptionService.saveSysLogEx(sysLogExceptionModel);
    }


    /**
     * 获取操作日志和异常日志比率
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/getLogRatio")
    @ApiOperation(value = "获取操作日志和异常日志比率")
    public CommonResult getLogRatio(@ApiParam("开始时间") String startTime,@ApiParam("结束时间") String endTime) throws CommonException {
        HashMap<Object, Object> map = new HashMap<>();
        Integer integer = sysLogService.selectSysLogByTime(startTime,endTime);
        Integer integer1 = sysLogExceptionService.selectSysLogByTime(startTime,endTime);
        map.put("normalLog",integer);
        map.put("abnormalLog",integer1);
        return CommonResult.ok(map);
    }


    /**
     * 获取时间内所有用户操作的次数
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/getLogAnalysis")
    @ApiOperation(value = "获取时间内所有用户操作的次数")
    public CommonResult getLogAnalysis(@ApiParam("开始时间") String startTime,@ApiParam("结束时间") String endTime) throws CommonException {
        List<Map<String, Object>> logAnalysis = sysLogService.getLogAnalysis(startTime, endTime);
        return CommonResult.ok(logAnalysis);
    }


    /**
     * 获取时间内所有用户操作的次数
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/getLoginIpCount")
    @ApiOperation(value = "获取时间内所有用户不同ip的登录次数")
    public CommonResult getLoginIpCount(String startTime, String endTime) throws CommonException {
        List<Map<String, Object>> logAnalysis = sysLogService.getLoginIpCount(startTime, endTime);
        return CommonResult.ok(logAnalysis);
    }

}
